package com.yami.shop.mystery.multishop.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.ExcelUtils;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.mystery.common.dto.ActivityWhiteDto;
import com.yami.shop.mystery.common.dto.WhiteUserDto;
import com.yami.shop.mystery.common.service.ActivityWhiteService;
import com.yami.shop.mystery.common.vo.ActivityWhiteExcle;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 白名单管理
 *
 * @author lgh
 */
@RestController
@RequestMapping("/activity/activityWhite")
@Tag(name="白名单接口")
public class ActivityWhiteController {

    private String[] whiteList = {"手机号","优先购数量","优惠购数量","大众购买数量"};

    @Autowired
    private ActivityWhiteService activityWhiteService;

    @GetMapping("/getList")
    @Operation(summary =  "分页查找白名单列表信息")
    public ServerResponseEntity<IPage<ActivityWhiteDto>> getList(PageParam<ActivityWhiteDto> page, String activityName){
        IPage<ActivityWhiteDto> pageWhite = activityWhiteService.getList(page,activityName);
        return ServerResponseEntity.success(pageWhite);
    }

    @PostMapping("/saveByList")
    @Operation(summary =  "导入活动白名单信息", description = "导入活动白名单信息")
    public ServerResponseEntity<Long> saveByList(@RequestParam("file") MultipartFile file, @RequestParam("activityId")Long activityId) {

        if (Objects.isNull(file)) {
            // 网络繁忙，请稍后重试
            throw new YamiShopBindException("yami.network.busy");
        }
        List<ActivityWhiteExcle> activityWhiteExcles = null;
        try{
            activityWhiteExcles = ExcelUtils.importExcel(file, 0, 1, ActivityWhiteExcle.class);
            //判断表头
            List<Object> objects = ExcelUtil.getReader(file.getInputStream()).read(0).get(0);
            List<String> titles = new ArrayList<>();
            titles.add("手机号");
            titles.add("优先购数量");
            titles.add("优惠购数量");
            titles.add("大众购买数量");
            for (Object title : objects){
                boolean contains = titles.contains(title.toString());
                if(!contains){
                    return ServerResponseEntity.showFailMsg("导入文件错误");
                }
            }
        }catch (Exception e){
            return ServerResponseEntity.showFailMsg("导入文件错误");
        }
        if(ObjectUtil.isEmpty(activityWhiteExcles)){
            throw new YamiShopBindException("导入白名单不能为空");
        }
        Long num = activityWhiteService.saveByList(activityWhiteExcles, activityId);
        return ServerResponseEntity.success(num);
    }

    @RequestMapping("saveByData")
    public ServerResponseEntity<Void> saveByData(@RequestParam("spuId") Long spuId,
                                                 @RequestParam("callPhone") String callPhone,
                                                 @RequestParam("numPurchase") Long numPurchase){
        List<ActivityWhiteExcle> activityWhiteExcles = new ArrayList<>();
        activityWhiteExcles.add(new ActivityWhiteExcle() {{
            setPhone(callPhone);
            setPurchaseNum(numPurchase);
        }});
        activityWhiteService.saveByList(activityWhiteExcles, spuId);
        return ServerResponseEntity.success();
    }

    @GetMapping("getWhiteUserList")
    public ServerResponseEntity<IPage<WhiteUserDto>> getWhiteUserList(PageParam<ActivityWhiteDto> page, @RequestParam("whiteId") Long whiteId, String phone){
        IPage<WhiteUserDto> whiteUserVoPageVO = activityWhiteService.getWhiteUserList(page,whiteId,phone);
        return ServerResponseEntity.success(whiteUserVoPageVO);
    }

    @GetMapping("updateStatus")
    public ServerResponseEntity<Boolean> updateStatus(@RequestParam("id") String id, @RequestParam("status") String status){
        Boolean falge = activityWhiteService.updateStatus(id,status);
        return ServerResponseEntity.success(falge);
    }

    @GetMapping(value = "/downloadWhiteList")
    @Operation(summary = "下载白名单导入模板")
    public void downloadWhiteList(HttpServletResponse response) {
        activityWhiteService.exportModel(response,whiteList,"");
    }

}
